#!/bin/sh
# Now using the util-linux losetup (losetup-FULL),
# and this script provides a compatibility layer to make
# its output look more like busybox losetup..
# - The output of both versions is a bit different
# - Older losetup versions require the -a param
# - Recent losetup versions don't require the -a param
#   but the output is bit different if -a is not specified
# - busybox losetup requires the -a param
# - Older versions of busybox losetup don't support the -a param

if [ -x /sbin/losetup-FULL ] ; then
	if [ "$1" = "" ] ; then
		RETSTUFF="`losetup-FULL -a`"
		RETVAL=$?
	else
		RETSTUFF="`losetup-FULL "$@"`"
		RETVAL=$?
	fi
	# /dev/loop/0: [2049]:194317 (/initrd/mnt/dev_save/PUPPYBOOT/precise/puppy_precise_5.7.1.sfs)
	# -> /dev/loop0: [2049]:194317 /initrd/mnt/dev_save/PUPPYBOOT/precise/puppy_precise_5.7.1.sfs
	# (/dev/loop/X = busybox (with bad config) in the initrd)
	echo "$RETSTUFF" | sed -e 's% (/% /%' -e 's%)$%%' -e 's|/dev/loop/|/dev/loop|'
else
	#busybox losetup
	if [ "$1" = "-a" ] ; then
		ALLUSED="-a"
		busybox losetup $ALLUSED &>/dev/null || ALLUSED=""
		RETSTUFF="`busybox losetup $ALLUSED`"
		RETVAL=$?
	else
		RETSTUFF="`busybox losetup "$@"`"
		RETVAL=$?
	fi
	echo "$RETSTUFF" | sed -e 's|/dev/loop/|/dev/loop|'
fi

exit $RETVAL

### END ###

#-------------
# losetup 2.27
#-------------
## losetup -a
#/dev/loop0: [2049]:194315 (/initrd/mnt/dev_save/PUPPYBOOT/precise/puppy_precise_5.7.1.sfs)
#/dev/loop1: [2049]:194314 (/initrd/mnt/dev_save/PUPPYBOOT/precise/fdrv_precise_5.7.1.sfs)
#/dev/loop2: [2049]:194311 (/initrd/mnt/dev_save/PUPPYBOOT/precise/zdrv_precise_5.7.1.sfs)
#/dev/loop3: [2049]:194320 (/initrd/mnt/dev_save/PUPPYBOOT/precise/ydrv_precise_5.7.1.sfs)
#/dev/loop4: [2049]:194306 (/initrd/mnt/dev_save/PUPPYBOOT/precise/adrv_precise_5.7.1.sfs)
#/dev/loop5: [2049]:194308 (/initrd/mnt/dev_save/PUPPYBOOT/precise/precisesave.4fs)
#/dev/loop6: [2049]:194307 (/initrd/mnt/dev_save/PUPPYBOOT/precise/devx_precise_5.7.1.sfs)

#---------------
# losetup 2.20.1
#---------------
## losetup -a
#/dev/loop0: [0801]:194315 (/mnt/dev_save/PUPPYBOOT/precise/puppy_precise_5.7.1.sfs)
#/dev/loop1: [0801]:194314 (/mnt/dev_save/PUPPYBOOT/precise/fdrv_precise_5.7.1.sfs)
#/dev/loop2: [0801]:194311 (/mnt/dev_save/PUPPYBOOT/precise/zdrv_precise_5.7.1.sfs)
#/dev/loop3: [0801]:194320 (/mnt/dev_save/PUPPYBOOT/precise/ydrv_precise_5.7.1.sfs)
#/dev/loop4: [0801]:194306 (/mnt/dev_save/PUPPYBOOT/precise/adrv_precise_5.7.1.sfs)
#/dev/loop5: [0801]:194308 (/mnt/dev_save/PUPPYBOOT/precise/precisesave.4fs)
#/dev/loop6: [0801]:194307 (/initrd/mnt/dev_save/PUPPYBOOT/precise/devx_precise_5.7.1.sfs)

#-------------
# busybox 1.25
#-------------
## busybox losetup -a
#/dev/loop0: 0 /mnt/dev_save/PUPPYBOOT/precise/puppy_precise_5.7.1.sfs
#/dev/loop1: 0 /mnt/dev_save/PUPPYBOOT/precise/fdrv_precise_5.7.1.sfs
#/dev/loop2: 0 /mnt/dev_save/PUPPYBOOT/precise/zdrv_precise_5.7.1.sfs
#/dev/loop3: 0 /mnt/dev_save/PUPPYBOOT/precise/ydrv_precise_5.7.1.sfs
#/dev/loop4: 0 /mnt/dev_save/PUPPYBOOT/precise/adrv_precise_5.7.1.sfs
#/dev/loop5: 0 /mnt/dev_save/PUPPYBOOT/precise/precisesave.4fs
#/dev/loop6: 0 /initrd/mnt/dev_save/PUPPYBOOT/precise/devx_precise_5.7.1.sfs

#-------------
# busybox 1.21
#-------------
## busybox losetup
#/dev/loop0: 0 /mnt/dev_save/PUPPYBOOT/precise/puppy_precise_5.7.1.sfs
#/dev/loop1: 0 /mnt/dev_save/PUPPYBOOT/precise/fdrv_precise_5.7.1.sfs
#/dev/loop2: 0 /mnt/dev_save/PUPPYBOOT/precise/zdrv_precise_5.7.1.sfs
#/dev/loop3: 0 /mnt/dev_save/PUPPYBOOT/precise/ydrv_precise_5.7.1.sfs
#/dev/loop4: 0 /mnt/dev_save/PUPPYBOOT/precise/adrv_precise_5.7.1.sfs
#/dev/loop5: 0 /mnt/dev_save/PUPPYBOOT/precise/precisesave.4fs
#/dev/loop6: 0 /initrd/mnt/dev_save/PUPPYBOOT/precise/devx_precise_5.7.1.sfs

